package com.cjm;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName E7Server
 * @Description TODO   内网E7服务
 * @Author admin
 * @Date 2020/8/5 11:44
 * @Version 1.0
 **/
public class E7Server {

    public static void main(String[] args) throws IOException {
        int serverPort = 5052;
        int clientNo = 1;

        ServerSocket serverSocket = new ServerSocket(serverPort);

        // 创建线程池
        ExecutorService exec = Executors.newCachedThreadPool();

        try {
            while (true) {
                Socket socket = serverSocket.accept();
                // 判断是否是穿透服务器的连接
                boolean isReq = socket.getInetAddress().toString().trim().equalsIgnoreCase("/192.168.15.170");
                if (isReq) {
                    // 每个客户端的请求都由一个独立的线程进行处理
                    exec.execute(new SingleE7Server(socket, clientNo));
                    clientNo++;
                }
            }
        } finally {
            serverSocket.close();
        }
    }

}

class SingleE7Server implements Runnable {

    private Socket socket;
    private int clientNo;

    public SingleE7Server(Socket socket, int clientNo) {
        this.socket = socket;
        this.clientNo = clientNo;
    }

    @Override
    public void run() {

        try {
            BufferedReader dataReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
            OutputStream outStream = socket.getOutputStream();

            do {

                /*
                 * TODO 判断是否带特定服务的指令
                 *  如果是 请求转发到特定内网
                 */
                // E7客户端
                System.out.println("********穿透服务获取E7服务**********");
                // 发送连接成功的状态码给E7客户端
               String strHost = "www.baidu.com";
               InetSocketAddress socketAddress = new InetSocketAddress(strHost, 80);
               Socket socket = new Socket();
               socket.connect(socketAddress);
               String strSend = "GET / HTTP/1.1\r\nHost: " + strHost + "\r\nConnection: Close\r\n\r\n";
               OutputStream outputStream = socket.getOutputStream();
               InputStream inputStream = socket.getInputStream();
               byte[] bytes = strSend.getBytes();
               outputStream.write(bytes);
               outputStream.flush();

               InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
               BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
               String line;
               while ((line = bufferedReader.readLine()) != null) {
                   outStream.write(line.getBytes());
                   outStream.flush();
//                   System.out.println(line + "\r\n");
               }

               bufferedReader.close();
               inputStreamReader.close();
               inputStream.close();
               outputStream.close();


            } while (dataReader.readLine() != null);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("与客户端" + clientNo + "通信结束");
//            try {
//                socket.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }

    }
}
